// pages/member/member.js
var upload = require('../../utils/upload.js');
var validate = require('../../utils/validate.js');
var commonapi = require('../../component/service/commonapi.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userDesc:{},
    uploadTask:{      
      uploadLicensePFinish:0,
      uploadLicenseFinish:0
    },
    isView:false,
    canuseprocess:false //能否用进度条
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(typeof(options.isView)!='undefined'){
      this.data.isView = options.isView;
      this.setData({
        isView:this.data.isView
      })
    }    
    this.getData();
    if(commonapi.verifySDKVersionNoRemind(1.4)){
      this.setData({
        canuseprocess:true
      })
    }
  },
  getData:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'webapp/control/userDesc.html',
      header: getApp().globalData.header,
      success: function (res) {
        if(typeof(res.data)!='undefined'&&res.data.sysNo>0){
          that.setData({
            userDesc: res.data
          })
        } 
      }
    })
  },
  bindName:function(e){
    this.data.userDesc.corpName = e.detail.value;
    this.setData({
       userDesc: this.data.userDesc
    })
  },
  bindLicense:function(e){
    this.data.userDesc.corpRegistrationNo = e.detail.value;
    this.setData({
       userDesc: this.data.userDesc
    })
  },
  uploadLicenseP:function(){
    wx.showLoading({
      title: '处理中...',
    })
    if(this.data.canuseprocess)
      upload.uploadImgProcess(this,'uploadLicensePFinish');
    else
      upload.uploadImg(this,'uploadLicensePFinish');
  },
  uploadLicense:function(){
    wx.showLoading({
      title: '处理中...',
    })
    if(this.data.canuseprocess)
      upload.uploadImgProcess(this,'uploadLicenseFinish');
    else
      upload.uploadImg(this,'uploadLicenseFinish');
  },
  uploadLicensePFinish:function(attachment){
    wx.hideLoading();
    if(typeof(attachment)!='undefined'){
      this.data.userDesc.licensePersonImgUrl = getApp().globalData.imgHost + attachment.outFileUrl;
      this.data.userDesc.corpImageRegistrationPerson = attachment.id;
      this.setData({
        userDesc: this.data.userDesc
      })
    }
  },
  uploadLicenseFinish:function(attachment){
    wx.hideLoading();
    if(typeof(attachment)!='undefined'){
      this.data.userDesc.licenseImgUrl = getApp().globalData.imgHost + attachment.outFileUrl;
      this.data.userDesc.corpImageRegistrationNo = attachment.id;
      this.setData({
        userDesc: this.data.userDesc
      })
    }
  },
  //提交审核
  doAction:function(){
    var that = this;
    if(!validate.validateEmpty(this.data.userDesc.corpName,'企业名称不得为空'))
      return;
    if(!validate.validateEmpty(this.data.userDesc.corpRegistrationNo,'营业执照注册号不得为空'))
      return;
    if(!validate.validateEmpty(this.data.userDesc.corpImageRegistrationPerson,'请上传手持营业执照照片'))
      return;
    if(!validate.validateEmpty(this.data.userDesc.corpImageRegistrationNo,'请上传营业执照照片'))
      return;
    wx.request({
      url: getApp().globalData.host + 'bizportal/my/user/saveWechat',
      //url: 'http://127.0.0.1:9080/bizportal/my/user/saveWechat',
      data: { corpName: that.data.userDesc.corpName,corpRegistrationNo:that.data.userDesc.corpRegistrationNo,
      corpImageRegistrationPerson:that.data.userDesc.corpImageRegistrationPerson,corpImageRegistrationNo:that.data.userDesc.corpImageRegistrationNo},
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        if(res.data.errorCode==0){
          wx.showToast({
            title: "已提交审核",
          })
          //延迟一秒返回前页（显示提示信息）
          setTimeout(function () {
            wx.navigateBack();
          }, 1000)
        }else{
          wx.showToast({
            title: "提交失败",
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})